package com.dqhl.stable.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.dqhl.stable.R;
import com.dqhl.stable.base.BaseActivity;
import com.dqhl.stable.listener.LightListener;
import com.dqhl.stable.util.Config;
import com.dqhl.stable.util.Dlog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by cheng on 2016/10/11.
 * 光照感应测试
 */
public class LightSensorActivity extends BaseActivity {

    private AMapLocationClient aMapLocationClient = null;
    private double curLongitude; //当前经度
    private double curLatitude; //当前纬度

    private TextView tv_content;
    private TextView tv_have_light;
    private LightListener lightListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);
        tv_content = (TextView) findViewById(R.id.tv_content);
        tv_have_light = (TextView) findViewById(R.id.tv_have_light);

        initAMap();

        lightListener = new LightListener(this);
        lightListener.setOnLightListener(new LightListener.onLightListener() {
            @Override
            public void onLight(float acc, float lux) {
                tv_content.setText("精度acc:" + acc + ";" + "光线强度lux：" + lux);
                Dlog.e("精度acc:" + acc + ";" + "光线强度lux：" + lux);
                if (lux > 2000) {
                    tv_have_light.setText("有光照");
                    // TODO 向服务器发送警报
                    sendMsg2Server();
                } else {
                    tv_have_light.setText("");
                }
            }
        });

    }

    /**
     * 初始化地图
     */
    private void initAMap() {
        aMapLocationClient = app.getaMapLocationClient();
        aMapLocationClient.setLocationListener(mLocationListener);
        aMapLocationClient.startLocation();
    }

    AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                //解析定位结果
//                String result = AMapUtils.getLocationStr(aMapLocation);
                curLongitude = aMapLocation.getLongitude();
                curLatitude = aMapLocation.getLatitude();
                Dlog.e("定位成功_经纬度：" + curLongitude + "," + curLatitude);
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Dlog.e("AmapErr:" + errText);
            }
        }
    };

    /**
     * 有光照时向服务器发送消息
     */
    private void sendMsg2Server() {
        String case_point = curLongitude + "," + curLatitude; //箱子的坐标
        Dlog.e("箱子的坐标:" + case_point);
        RequestParams params = new RequestParams(Config.Api.warning);
        params.addBodyParameter("case_state", "4"); //箱子的状态
        params.addBodyParameter("case_sign", "2"); //箱子号
        params.addBodyParameter("case_point", case_point); //箱子的坐标
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                lightListener.start();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    protected void onPause() {
        if (lightListener != null) {
            lightListener.stop();
        }
        Dlog.e("lightListener___stop");
        super.onPause();
    }

    @Override
    protected void onResume() {
        lightListener.start();
        Dlog.e("lightListener___start");
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (lightListener != null){
            lightListener.stop();
        }
        super.onDestroy();
    }
}
